package com.marktotop.service;

import com.marktotop.entity.Student;
import com.marktotop.utils.Dom4jUtil;
import org.dom4j.Document;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * 通过逻辑代码 调用 Dom4jUtil 来处理 CRUD
 */
public class StudentManager {

    private String path;

    public StudentManager() {
        super();
    }

    public StudentManager(String path) {
        super();
        this.path = path;
    }


    /**
     * 根据 id 查找 学生信息
     * @param id
     * @throws Exception
     */
    public Student findById(String id) throws Exception {
        // 获得person根节点
        Document doc = Dom4jUtil.getDocument(path);
        Element root = doc.getRootElement();
        // 得到所有student节点
        List<Element> list = root.elements("student");
        // 遍历student节点
        for (Element element : list) {
            // 获取所选id值，并输出其姓名年龄和所在地
            if (element.element("id").getText().equals(id)) {
                Student student = new Student()
                        .setId(element.element("id").getText())
                        .setName(element.element("name").getText())
                        .setSex(element.element("sex").getText())
                        .setAge(element.element("age").getText())
                        .setGradeName(element.element("gradeName").getText())
                        .setMajor(element.element("major").getText())
                        .setContact(element.element("contact").getText())
                        .setAddress(element.element("address").getText());
                // 返回 Student 对象
                return student;
            }
        }
        return null;
    }


    /**
     * 添加学生信息
     * @param student 对象
     * @throws Exception
     */
    public void addStudent(Student student) throws Exception {
        // 获得 student 根节点
        Document doc = Dom4jUtil.getDocument(path);
        Element root = doc.getRootElement();
        // 根节点上添加student标签
        Element stu = root.addElement("student");
        // 给student标签里面添加各种标签并赋值
        stu.addElement("id").setText(student.getId());
        stu.addElement("name").setText(student.getName());
        stu.addElement("sex").setText(student.getSex());
        stu.addElement("age").setText(student.getAge());
        stu.addElement("gradeName").setText(student.getGradeName());
        stu.addElement("major").setText(student.getMajor());
        stu.addElement("contact").setText(student.getContact());
        stu.addElement("address").setText(student.getAddress());

        // 回写xml
        Dom4jUtil.ReWrite(path, doc);
    }


    /**
     * 根据id删除对应学生信息
     * @param id 学号
     * @return
     * @throws Exception
     */
    public boolean removeById(String id) throws Exception {
        // 定义一个变量判断是否删除成功，返回false则代表没有此id
        boolean boo = false;
        // 获得person根节点
        Document doc = Dom4jUtil.getDocument(path);
        Element root = doc.getRootElement();
        // 得到所有student节点
        List<Element> list = root.elements("student");
        // 遍历student节点
        for (Element element : list) {
            if (element.element("id").getText().equals(id)) {
                boo = true;
                element.getParent().remove(element);
                break;
            }
        }
        Dom4jUtil.ReWrite(path, doc);
        return boo;
    }


    /**
     * 查询所有学生信息
     * @return 所有学生信息
     * @throws Exception
     */
    public List<Student> findAll() throws Exception {
        // 获取document对象
        Document doc = Dom4jUtil.getDocument(path);
        // 获取根节点
        Element root = doc.getRootElement();
        // 获取孩子节点
        List<Element> list = root.elements("student");
        // 创建集合
        Student student;
        // 遍历孩子节点
        ArrayList<Student> students = new ArrayList<Student>();
        for (Element element : list) {
            student = new Student();
            student.setId(element.element("id").getText());
            student.setName(element.element("name").getText());
            student.setSex(element.element("sex").getText());
            student.setAge(element.element("age").getText());
            student.setGradeName(element.element("gradeName").getText());
            student.setMajor(element.element("major").getText());
            student.setContact(element.element("contact").getText());
            student.setAddress(element.element("address").getText());
            students.add(student);
        }
        return students;
    }


    /**
     * 修改学生信息
     * @param student 对象
     * @throws Exception
     */
    public boolean updateStudent(Student student) throws Exception {
        // 获取document对象
        Document doc = Dom4jUtil.getDocument(path);
        // 获取根节点
        Element root = doc.getRootElement();
        // 获取子节点
        List<Element> list = root.elements();
        // 获取student对象
        Student upStudent = new Student();
        // 遍历
        for (Element element : list) {
            // 获取节点文本值
            String dId = element.element("id").getText();

            // 验证id是否相等
            if (dId.equals(student.getId())){
                if(student.getName()!= null){
                    element.element("name").setText(student.getName());
                }
                if(student.getAge()!= null){
                    element.element("age").setText(student.getAge());
                }
                if(student.getSex()!= null){
                    element.element("sex").setText(student.getSex());
                }
                if(student.getGradeName()!= null){
                    element.element("gradeName").setText(student.getGradeName());
                }
//                element.element("age").setText(student.getAge().toString());
                if(student.getMajor()!= null){
                    element.element("major").setText(student.getMajor());
                }
                if(student.getContact()!= null){
                    element.element("contact").setText(student.getContact());
                }
                if(student.getAddress()!= null){
                    element.element("address").setText(student.getAddress());
                }

                Dom4jUtil.ReWrite(path,doc);
                return true;
            }
        }
    return false;
    }
}
